<?php require_once('../../Connections/cotiza.php'); ?>
<?php
session_start();
$campo    = $_GET['campo'];
$acc      = $_GET['acc'];
$solic    = $_GET['id_s'];
$vendedor = $_SESSION['usuario'];
$permisos = $_SESSION['permisos'];

mysql_select_db($database_cotiza, $cotiza);
		
if($acc == "search")
{
	if($permisos == 1 or $permisos == 0)
		$condicion = " AND solic_user_c = '$vendedor'";
	
	if($solic <> "")
		$condicion .= " AND solic_id <> '$solic'";
		
	$b = $_POST['busqueda'];
	$query = "SELECT solic_cliente, solic_id, cm_sort, solic_npcliente, solic_fecha, solic_user_c, status_desc 
	            FROM solic_mstr, cm_mstr, status_mstr
	           WHERE cm_rut = solic_cliente AND status_id = solic_status
			     AND (solic_id like '%$b%' or cm_sort like '%$b%' or solic_npcliente like '%$b%') 
			     AND solic_status = 0".$condicion." 			
			ORDER BY solic_id";
}
else
{
	if($permisos == 1 or $permisos == 0)
		$condicion = " AND solic_user_c = '$vendedor'";
	
	if($solic <> "")
			$condicion = " AND solic_id <> '$solic'";
		
	$query = "SELECT solic_cliente, solic_id, solic_npcliente, solic_fecha, solic_user_c, status_desc
	            FROM solic_mstr, cm_mstr, status_mstr
	           WHERE cm_rut = solic_cliente AND status_id = solic_status
			     AND cm_rut = solic_cliente 
				 AND solic_status = 0".$condicion." 
		    ORDER BY solic_id";
			//echo $query;
}
$busqueda = mysql_query($query, $cotiza) or die(mysql_error());
$row = mysql_fetch_assoc($busqueda);
$totalRows = mysql_num_rows($busqueda);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>..:: Solicitudes ::..</title>
<link href="../../main.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.default_title {
	color: #FFF;
}
-->
</style>
</head>

<body onload="document.form1.busqueda.focus();">
<form id="form1" name="form1" method="post" action="solicitudes_sol.php?campo=<?php echo $campo; ?>&acc=search&id_s=<?php echo $solic;?>">
  <table border="0" align="center">
    <tr>
      <td >B&uacute;squeda de Solicitudes:</td>
      <td ><input name="busqueda" class="default" size="20" autocomplete="off" /></td>
      <td ><input type="submit" value="Buscar" class="v8" /></td>
    </tr>
  </table>
</form>
<br />
<?php if($totalRows >0){?>
  <table border="1" align="center" cellpadding="2" cellspacing="0">
    <tr class="default_title">
      <td width="60" align="center" bgcolor="#0000CC">Id Solicitud</td>
      <td width="75" align="center" bgcolor="#0000CC">Fecha</td>
      <td width="80" align="center" bgcolor="#0000CC">Rut</td>
      <td width="202" align="center" bgcolor="#0000CC">Cliente</td>
      <td width="153" align="center" bgcolor="#0000CC">Producto</td>
      <?php if($permisos <> 1 and $permisos <> 0) {?>
      <td width="50" align="center" bgcolor="#0000CC">Estado</td>
      <td width="50" align="center" bgcolor="#0000CC">Usuario</td>
      <?php }?>
    </tr>
    <?php do { 
			$cliente = $row['solic_cliente'];
			
			$q_client  = "SELECT * FROM cm_mstr WHERE cm_rut = '$cliente'";
			$rst_cte   = mysql_query($q_client,$cotiza);
			$row_cte   = mysql_fetch_assoc($rst_cte);
		
			//$registros = mysql_num_rows($rst_cte);
			//if($registros > )
			
			//$row_cte   = mysql_fetch_assoc($rst_cte);
	?>
    <tr>
      <td><a href="javascript:window.opener.document.getElementById('<?php echo $campo; ?>').value= '<?php echo $row['solic_id']; ?>';window.opener.document.getElementById('<?php echo $campo; ?>').focus();window.close();"> <?php echo $row['solic_id']; ?> </a>&nbsp;</td>
      <td><?php echo $row['solic_fecha']; ?>&nbsp;</td>
      <td><?php echo $row_cte['cm_rut']; ?>&nbsp;</td>
      <td><?php echo $row_cte['cm_sort']; ?>&nbsp;</td>
      <td><?php echo $row['solic_npcliente']; ?>&nbsp;</td>
      <?php if($permisos <> 1 and $permisos <> 0) {?>
      <td><?php echo $row['status_desc']; ?>&nbsp;</td>
      <td><?php echo $row['solic_user_c']; ?>&nbsp;</td>
      <?php }?>
    </tr>
      <?php } while ($row = mysql_fetch_assoc($busqueda)); ?>
  </table>
<?php }

		$query    = "SELECT * FROM solic_mstr WHERE solic_status = 0 AND solic_devolver_a = 'v' ".$condicion;
		$rslt_rec = mysql_query($query,$cotiza);
		$row_rec  = mysql_fetch_assoc($rslt_rec);
		$cnt_rec  = mysql_num_rows($rslt_rec);
		
		if($cnt_rec >=1)
		{
?>
<br />
<br />
<strong>Solicitudes Rechazadas</strong><br />
<table width="714" border="0" align="center">
  <tr>
    <td width="708" align="left"><table border="1" align="left" cellpadding="2" cellspacing="0">
      <tr class="default_title">
        <td width="60" align="center" bgcolor="#0000CC">Id Solicitud</td>
        <td width="400" align="center" bgcolor="#0000CC">Motivo</td>
        <?php if($permisos <> 1 and $permisos <> 0) {?>
        <td width="50" align="center" bgcolor="#0000CC">Usuario</td>
        <?php }?>
      </tr>
      <?php
 	do{
 ?>
      <tr>
        <td><a href="javascript:window.opener.document.getElementById('<?php echo $campo; ?>').value= '<?php echo $row_rec['solic_id']; ?>';window.opener.document.getElementById('<?php echo $campo; ?>').focus();window.close();"> <?php echo $row_rec['solic_id']; ?></a>&nbsp;</td>
        <td><?php echo $row_rec['solic_devolucion']; ?>&nbsp;</td>
        <?php if($permisos <> 1 and $permisos <> 0) {?>
        <td width="50" ><?php echo $row_rec['solic_user_c']; ?></td>
        <?php }?>
      </tr>
      <?php
	}while($row_rec  = mysql_fetch_assoc($rslt_rec));
  ?>
    </table></td>
  </tr>
</table>
<?php }//if($cnt_rec >=1)?>
<?php 	$query    = "SELECT * FROM solic_mstr WHERE solic_cliente IS NULL".$condicion;
		$rslt_bca = mysql_query($query,$cotiza);
		$row_bca  = mysql_fetch_assoc($rslt_bca);
		$cnt_bca  = mysql_num_rows($rslt_bca);
		
		if($cnt_bca >=1 && "1"=="0")
		{
?>
<br />
<br />
<strong>Solicitudes En Blanco</strong><br />
<table width="714" border="0" align="center">
  <tr>
    <td width="708" align="left"><table border="1" align="left" cellpadding="2" cellspacing="0">
      <tr class="default_title">
        <td width="60" align="center" bgcolor="#0000CC">Id Solicitud</td>
        <td width="75" align="center" bgcolor="#0000CC">Fecha</td>
        <?php if($permisos <> 1 and $permisos <> 0) {?>
        <td width="50" align="center" bgcolor="#0000CC">Usuario</td>
        <?php }?>
      </tr>
      <?php
 	do{
 ?>
      <tr>
        <td><a href="javascript:window.opener.document.getElementById('<?php echo $campo; ?>').value= '<?php echo $row_bca['solic_id']; ?>';window.opener.document.getElementById('<?php echo $campo; ?>').focus();window.close();"> <?php echo $row_bca['solic_id']; ?></a>&nbsp;</td>
        <td><?php echo $row_bca['solic_fecha']; ?>&nbsp;</td>
        <?php if($permisos <> 1 and $permisos <> 0) {?>
        <td width="50" ><?php echo $row_bca['solic_user_c']; ?></td>
        <?php }?>
      </tr>
      <?php
	}while($row_bca  = mysql_fetch_assoc($rslt_bca));
  ?>
    </table></td>
  </tr>
</table>
<?php }//if($cnt_bca >=1)?>
</body>
</html>
<?php 
mysql_close($cotiza);
?>